package com.suitsoft.ut.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;


/**
 * WORD工具类
 *@author Suine
 */
public class WordUtils {
	/** 模拟替换KEY */
	private final String MOBAN_TIHUAN_KEY = "#{cccccxx}";
	
	private String srcPah;
	private String conteTempPath;
	/**
	 * 初始化
	 * @param srcPah doc路径
	 * @param conteTempPath 内容模板路径
	 * @param desPath 生成路径
	 */
	public WordUtils(String srcPah,String conteTempPath){
		this.srcPah = srcPah;
		this.conteTempPath = conteTempPath;
	}
	/**
	 * 生成一个word(web)文件
	 * <pre>遍历values 同时 替换 内容模板</pre>
	 * @param srcPah WORD模板  相当于html的html和body部分
	 * @param conteTempPath  内容模板 相当于html的body里面的部分
	 * @param desPath 输出路径
	 * @param values 
	 */
	public void searchAndReplace(String desPath,List<Map<String,String>> values){
		File srcfile = new File(srcPah);
		File desfile = new File(conteTempPath);
		BufferedReader reader = null;
		//内容模板
		String contentTemp = "";
		//word模板
		String wordTemp = "";
		try {
			//选读出work模板文件
			InputStreamReader read = new InputStreamReader(new FileInputStream(srcfile),"gb2312");

			reader = new BufferedReader(read);
			String tempStr = null;
			StringBuffer sb = new StringBuffer();
			while((tempStr = reader.readLine()) != null){
				sb.append(tempStr + "\n");
			}
			read.close();
			wordTemp = sb.toString();
			
			System.out.println(wordTemp);
			
			//选读出内容模板文件
			tempStr = null;
			sb = new StringBuffer();
			read = new InputStreamReader(new FileInputStream(desfile),"UTF-8");
			reader = new BufferedReader(read);
			while((tempStr = reader.readLine()) != null){
				sb.append(tempStr + "\n");
			}
			read.close();
			contentTemp = sb.toString();
			
			
			//替换内容模板的内容
			String temp = "";
			String temp1 = "";
			for(Map<String,String> m : values){
				temp1 = contentTemp;
				for(Map.Entry<String, String>mm : m.entrySet()){
					temp1 = temp1.replace(mm.getKey(), mm.getValue());
				}
				temp += temp1;
			}
			
			//把替换好的内容   在word模板中替换
			wordTemp = wordTemp.replace(MOBAN_TIHUAN_KEY, temp);
			System.out.println(wordTemp);
			
			FileOutputStream fos = new FileOutputStream(new File(desPath));
			OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-16");
			osw.write(wordTemp);
			osw.flush();
			osw.close();
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Map<String,String> values = new HashMap<String,String>();
		String srcPath = "d:\\task.doc";
		String desPath = "d:\\task-new.doc";
		String contentTemple = "d:\\task-content";
		WordUtils utils = new WordUtils(srcPath,contentTemple);
		
		values.put("#{titile}", "题目");
		List<Map<String,String>> list = Lists.newArrayList();
		list.add(values);
		values = new HashMap<String,String>();
		values.put("#{titile}", "题目1");
		list.add(values);
		
		utils.searchAndReplace(desPath,list);
	}
}

